package com.spring.nio;

import com.spring.nio.common.OsInfo;
import com.spring.nio.core.startup.NioServerContext;
import com.spring.nio.netty.NettyServer;
import com.spring.nio.util.LocalTime;
import com.spring.nio.verify.ClientNoSoLingerThread;
import com.spring.nio.verify.ClientSoLingerThread;
import com.spring.nio.verify.ServerNoSoLingerThread;
import com.spring.nio.verify.ServerSoLingerThread;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @Author: spring
 * @Date: 2021/02/13 00:00:30
 * @Pacage_name: com.alibaba.server
 * @Project_Name: nio-server
 * @Description: 网络服务服务端启动类
 */

@Slf4j
public class NioServerStart {
    public static void main( String[] args ) {
        log.info("[" + LocalTime.formatDate(LocalDateTime.now()) + "] App | --> 当前操作系统类型: " + OsInfo.getOSName() + ", 可支持的最大线程数: " + Runtime.getRuntime().availableProcessors());

        // 1、启动Nio服务
        //
         NioServerContext.startupServerContext();

        // 2、启动Netty服务
        //NettyServer.startup();

        //test();
    }

    private static void test() {
        try {
            // 客户端、服务端均未设置soLinger选项
            /*new Thread(new ServerNoSoLingerThread()).start();
            TimeUnit.MILLISECONDS.sleep(5000);
            new Thread(new ClientNoSoLingerThread()).start();*/

            // 客户端设置soLinger选项，服务端未设置
            /*new Thread(new ServerNoSoLingerThread()).start();
            TimeUnit.MILLISECONDS.sleep(5000);
            new Thread(new ClientSoLingerThread()).start();*/

            // 客户端未设置，服务端设置soLinger选项
            /*new Thread(new ServerSoLingerThread()).start();
            TimeUnit.MILLISECONDS.sleep(5000);
            new Thread(new ClientNoSoLingerThread()).start();*/

            // 客户端、服务端均设置
            new Thread(new ServerSoLingerThread()).start();
            TimeUnit.MILLISECONDS.sleep(5000);
            new Thread(new ClientSoLingerThread()).start();

            Thread.currentThread().join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
